package com.carkeeper.user.dao;

import com.carkeeper.user.pojo.Permission;
import com.carkeeper.user.pojo.Role;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;
import java.util.Set;

@Repository
public interface PermissionDao extends Mapper<Permission> {

    /**
     * 根据角色id和权限id为中间添加/修改数据
     * @param roleId
     * @param permissionId
     * @return
     */
    @Insert("insert into tb_role_permission values (#{roleId}, #{permissionId})")
    int addPermissionByRoleId(@Param("roleId") Integer roleId, @Param("permissionId") Integer permissionId);

    /**
     * 根据角色id,权限id查询角色-权限中间表
     * @param roleId
     * @return
     */
    @Select("select * from tb_permission where id in (select permission_id from tb_role_permission where role_id = #{roleId})")
    // @Select("select * from tb_role_permission where role_id = #{roleId}")
    Set<Permission> findPermissionByRoleId(@Param("roleId") Integer roleId);

    @Delete("delete from tb_role_permission where role_id = #{roleId}")
    /**
     * 根据角色id,权限id删除角色-权限中间表
     * @param roleId
     * @return
     */
    int deletePermissionByRoleId(@Param("roleId") Integer roleId);
}
